﻿using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Comkarl.Restful.Shell.External;
using SampleCommands.Classes;

namespace SampleCommands
{
    [ShellCommandName("windsor-ioc-command")]
    public sealed class WindsorIoCCommand : ShellCommandBase
    {
        public override void Execute(ShellResponse response)
        {
            /* Note you need to execute anything related to Castle Windsor as a critical operation
             * because this library does not allow partially-trusted callers, which is the case
             * of a shell command module!
             */
            WindsorContainer container = ShellUtility.Critical(() => new WindsorContainer());
            ShellUtility.Critical(() => container.Register(Component.For(typeof(IComponent)).ImplementedBy(typeof(SampleComponent)).Named("Components.SampleComponent")));
            IComponent component = ShellUtility.Critical(() => container.Resolve<IComponent>());
            component.DoIt();

            /* Same thing but loading from a configuration file: */
            WindsorContainer container2 = ShellUtility.Critical(() => new WindsorContainer(new XmlInterpreter(ResolveDataPath("Container.config"))));
            IComponent component2 = ShellUtility.Critical(() => container2.Resolve<IComponent>());
            component2.DoIt();

            /* Or just use the container started by ShellCommandBase in this sample project */
            IComponent component3 = ShellUtility.Critical(() => Container.Current.Resolve<IComponent>());
            component3.DoIt();
        }
    }
}